#include <iostream>
#include <string>
#include <boost/filesystem.hpp>

#include "index_builder.h"
#include "search_manager.h"
#include "doc_parser.h"

int main(int argc, char *argv[])
{
    if (argc != 3) {
        std::cout << "search_engine " << "[-b doc_path]  or [-s query]\n";
        return -1;
    }

    std::string cmd(argv[1]);
    std::string para(argv[2]);

    if (cmd == "-b") {
        IndexBuilder indexBuilder("index.db", "doc.db");
        indexBuilder.LoadDB();

        // 遍历路径
        boost::filesystem::recursive_directory_iterator end;
        boost::filesystem::recursive_directory_iterator pos(".");

        while (pos != end) {
            if (!boost::filesystem::is_directory(*pos)) {
                std::string docPath = pos->path().string();
                std::string title;
                std::string body;

                // 获取title和body
                DocParser docParser(docPath);
                docParser.Parse(title, body);

                indexBuilder.AddDoc(title, body);
            }

            ++pos;
        }
    } else if (cmd == "-s") {
        SearchManager searchMgr("index.db", "doc.db");

        searchMgr.Search(para);
    } else {
        std::cout << "invalid parameter\n";
        return -1;
    }

    return 0;
}

